<?php
/*************************************************************************

  $Id: iclear.php 323 2009-08-19 11:06:28Z dis $

  iclear payment system - because secure is simply secure
  http://www.iclear.de

  Copyright (c) 2001 - 2009 iclear

  Released under the GNU General Public License

************************************************************************

                    All rights reserved.

  This program is free software licensed under the GNU General Public License (GPL).

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA

*************************************************************************/

  define('IC_SEC', true);
// $icCore is instantiated in IclearProxy inclusion
  global $icCore;
  
  require('./iclear/IclearProxy.php');
  
  // disable error reporting if we're not in debug mode
  if(!$icCore->debug()) {
    if(function_exists('error_reporting')) {
      error_reporting(0);
    }
    
    if(function_exists('ini_set')) {
      ini_set('display_errors', 0);
    }
  }
  
  /***************************************************************
   * Local configuration:
   * If there are special modules or constant needed in the shop
   * system rename / copy iclear/config.local.sample.php to
   * config.local.php and add the needed code there
  ***************************************************************/
  define('IC_CONFIG_LOCAL', $icCore->getPath('iclear') . 'config.local.php');
  if(file_exists(IC_CONFIG_LOCAL)) {
    require_once IC_CONFIG_LOCAL;
  }
  
  
  $icProxy = new IclearProxy($icCore);
  
  if(isset($_GET['wsdl'])) {
  	include($icCore->getPath('iclear') . 'ICCustomerServices.php');
  	exit;
  }
  
// SOAP stuff goes here
$server =& $icCore->getSoapServer('IclearSoapServer');
$server->init($icCore->getBaseURL() . basename($_SERVER['PHP_SELF']) . '?wsdl');

$HTTP_RAW_POST_DATA = file_get_contents("php://input");

if(IC_DEBUG_LOG && IC_DEBUG_LOG_FILE && $HTTP_RAW_POST_DATA) {
  if($fp = @fopen($icCore->getPath('iclear') . IC_DEBUG_LOG_FILE, 'w')) {
    fputs($fp, '<!-- START SOAP processing: ' . date('Y-m-d H:i:s') . "-->\n" . $HTTP_RAW_POST_DATA . "\n<!-- END -->\n");
    fclose($fp);
  }
}

// removing bogus namespace definitions - nusoap doesn't like it!
$HTTP_RAW_POST_DATA  = preg_replace('/xmlns=""/', '', $HTTP_RAW_POST_DATA);
$server->handle($HTTP_RAW_POST_DATA);

// mapped SOAP functions follows

  /**
   * patch 2 intercept broken SOAP definition of basketItemList
   * If there's only one item in basket, BasketItem is an array with all fields of that item
   * If there are multiple items in basket, BasketItem is a numerical array of arrays, each one a item
   *
   * @param array $basketItemList
   * @return array $basketItemList
   */
  function checkBasketItemList($basketItemList = false) {
    if($basketItemList && is_array($basketItemList)) {
      if(isset($basketItemList['BasketItem'][IC_SOAP_ITEM_NO])) {
        $basketItemList[0] = $basketItemList['BasketItem'];
        unset($basketItemList['BasketItem']);
      }elseif(isset($basketItemList['BasketItem']) && is_array($basketItemList['BasketItem'])) {
        $basketItemList = $basketItemList['BasketItem'];
      }
    }
    return $basketItemList;
  }


function validateBasket($sessionID, $basketID, $currency, $orderStatus, $orderStatusMessage, $requestID, $deliveryAddress, $basketItemList) {
  global $icCore;
  #$basketItemList = checkBasketItemList($basketItemList);
  $result = array(
    IC_SOAP_SESSION_ID   => $sessionID,
    IC_SOAP_BASKET_ID   => $basketID,
    IC_SOAP_CURRENCY   => $currency,
    IC_SOAP_ORDER_STATUS_ID => $orderStatus,
    IC_SOAP_ORDER_STATUS_MESSAGE => $orderStatusMessage,
    IC_SOAP_BASKET_ITEM_LIST => $basketItemList,
    IC_SOAP_DELIVERY_ADDRESS => $deliveryAddress,
    IC_SOAP_REQUEST_ID => $requestID
  );
  $basket = false;
  
  if($proxy =& $icCore->getProxy()) {
  	$basket =& $proxy->perform('loadBasket', array($basketID));
  }
  if(!$basket) {
    $proxy->addError('Specified basket not found');
  } else {
  	$basket->acceptOK($result);
  }
}


/**
 * SOAP acceptOrder function
 *
 * @param string $sessionID
 * @param string $basketID
 * @param string $currency
 * @param int $orderStatus
 * @param string $orderStatusMessage
 * @param int $requestID
 * @param array $deliveryAddress
 * @param array $basketItemList
 * @return array (SOAP)
 */
function acceptOrder($sessionID, $basketID, $currency, $orderStatus, $orderStatusMessage, $requestID, $deliveryAddress, $basketItemList) {
	global $icCore;
  validateBasket($sessionID, $basketID, $currency, $orderStatus, $orderStatusMessage, $requestID, $deliveryAddress, $basketItemList);

  $proxy =& $icCore->getProxy();
  $basket =& $icCore->getBasket();
  
  $out = array(
								IC_SOAP_SESSION_ID 	=> $basket->sessionID(),
								IC_SOAP_REQUEST_ID => $basket->requestID(),
								IC_SOAP_STATUS_ID 		=> $basket->errorCode(),
								IC_SOAP_STATUS_MESSAGE => $basket->errorCode() ? $basket->lastError() : 'OK',
								IC_SOAP_URL_SHOP => $proxy->perform('shopURL', array($basket->orderAccepted()))
						  );
						  
  if(IC_DEBUG_LOG && IC_DEBUG_LOG_FILE) {
    if($fp = @fopen($icCore->getPath('iclear') . IC_DEBUG_LOG_FILE, 'a')) {
      fputs($fp, '<!-- START acceptOrderResult: ' . date('Y-m-d H:i:s') . "-->\n" . print_r($out, true) . "\n<!-- END -->\n");
      fclose($fp);
    }
  }
  return $out;
}

function apiInfo($infoType = '') {
	global $icCore;
  $params = array();
	
  $iniFile = $icCore->getPath('iclear') . 'IclearConfig.ini';
	foreach(parse_ini_file($iniFile) AS $key => $val) {
		$params[] = array(
	    'key' => $key,
	    'value' => $val,
	    'label' => '',
		);
	}
	
  $proxy =& $icCore->getProxy();
  $params[] = array(
	  'key' => 'IC_API_ENABLED',
	  'value' => $proxy->perform('enabled') ? 'yes' : 'no',
	  'label' => '',
  );
                   
  $params[] = array(
	  'key' => 'IC_TABLE_EXIST',
	  'value' => $proxy->perform('tableExist') ? 'yes' : 'no',
	  'label' => '',
  );
                   
  $params[] = array(
	  'key' => 'IC_WRAPPER_ID',
	  'value' => $proxy->perform('id'),
	  'label' => '',
  );
                   
  $params[] = array(
	  'key' => 'IC_WRAPPER_VERSION',
	  'value' => $proxy->perform('version'),
	  'label' => '',
  );
  
  $params[] = array(
    'key' => 'IC_CORE_VERSION',
    'value' => $icCore->version(),
    'label' => '',
  );
  
  $params[] = array(
    'key' => 'IC_CONFIG_LOCAL',
    'value' => defined('IC_CONFIG_LOCAL_LOADED') ? 1 : 0,
    'label' => '',
  );
  
  $basket =& $icCore->getBasket();
  $params[] = array(
    'key' => 'IC_BASKET_VERSION',
    'value' => $basket->version(),
    'label' => '',
  );
  
  if(IC_DEBUG_LOG && IC_DEBUG_LOG_FILE) {
    if($fp = @fopen($icCore->getPath('iclear') . IC_DEBUG_LOG_FILE, 'a')) {
      fputs($fp, '<!-- START apiInfoResult: ' . date('Y-m-d H:i:s') . "-->\n" . print_r($params, true) . "\n<!-- END -->\n");
      fclose($fp);
    }
  }
  
  return $params;
}

function confirmShopBetreiberImp(
  $ExtCustomerID,
  $status,
  $statusMessage,
  $ICShopID,
  $requestID
) {
  global $icCore;
	$out = array(
	  IC_SOAP_STATUS_ID => 0,
	  IC_SOAP_STATUS_MESSAGE => 'OK',
    IC_SOAP_REQUEST_ID => $requestID,
	);
	
  if(IC_DEBUG_LOG && IC_DEBUG_LOG_FILE) {
    if($fp = @fopen($icCore->getPath('iclear') . IC_DEBUG_LOG_FILE, 'a')) {
      fputs($fp, '<!-- START acceptOrderResult: ' . date('Y-m-d H:i:s') . "-->\n" . print_r($out, true) . "\n<!-- END -->\n");
      fclose($fp);
    }
  }
	return $out;
}
?>